package noyau;

import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ServantNotActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;

import projetCORBA.Banque;
import projetCORBA.BanqueHelper;
import projetCORBA.Garant;
import projetCORBA.GarantHelper;
import projetCORBA.Stock;
import projetCORBA.StockHelper;

public class Serveur {

	public static void main(String[] args) {

		Securite secu = new Securite();

		org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

		try {
			org.omg.CORBA.Object objRef = orb
					.resolve_initial_references("NameService");
			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

			POA rootpoa = POAHelper.narrow(orb
					.resolve_initial_references("RootPOA"));
			rootpoa.the_POAManager().activate();

			BanqueImpl objb = new BanqueImpl(secu);

			org.omg.CORBA.Object refai = rootpoa.servant_to_reference(objb);

			Banque bref = BanqueHelper.narrow(refai);
			NameComponent path[];
			try {
				path = ncRef.to_name("BanqueJBCM");
				ncRef.rebind(path, bref);
			} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
				e.printStackTrace();
			} catch (NotFound e) {
				e.printStackTrace();
			} catch (CannotProceed e) {
				e.printStackTrace();
			}

			StockImpl objs = new StockImpl(secu);
			
			org.omg.CORBA.Object refs = rootpoa.servant_to_reference(objs);
			Stock sref = StockHelper.narrow(refs);
			try {
				path = ncRef.to_name("StockJBCM");
				ncRef.rebind(path, sref);
			} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
				e.printStackTrace();
			} catch (NotFound e) {
				e.printStackTrace();
			} catch (CannotProceed e) {
				e.printStackTrace();
			}
			
			GarantImpl objg = new GarantImpl(secu, objs, objb);
			
			org.omg.CORBA.Object refg = rootpoa.servant_to_reference(objg);
			Garant gref = GarantHelper.narrow(refg);
			try {
				path = ncRef.to_name("GarantJBCM");
				ncRef.rebind(path, gref);
			} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
				e.printStackTrace();
			} catch (NotFound e) {
				e.printStackTrace();
			} catch (CannotProceed e) {
				e.printStackTrace();
			}

			orb.run();
			
		} catch (InvalidName e) {
			e.printStackTrace();
		} catch (AdapterInactive e) {
			e.printStackTrace();
		} catch (ServantNotActive e) {
			e.printStackTrace();
		} catch (WrongPolicy e) {
			e.printStackTrace();
		}

	}
}
